package com.moon.controller;

import com.huaban.analysis.jieba.JiebaSegmenter;
import com.huaban.analysis.jieba.SegToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.moon.utils.BaseController;
import com.moon.entity.Poetry;
import com.moon.service.PoetryService;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/moon/Poetry")
public class PoetryController extends BaseController {
	@Autowired
	private PoetryService poetryService;

	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@RequestMapping("/selectByWord")
	@ResponseBody
	public List<Poetry> selectByWord(String word) {
		JiebaSegmenter seg = new JiebaSegmenter();
		List<SegToken> list = seg.process(word, JiebaSegmenter.SegMode.SEARCH);
		StringBuilder wheresql = new StringBuilder("where id in(");
		for (SegToken token : list) {
			SetOperations ops = stringRedisTemplate.opsForSet();
			Set<String> set = ops.members(token.word);
			for (String id : set) {
				wheresql.append(id + ",");
			}
		}
		wheresql.append("-1)");
		return poetryService.selectByWhereSql(wheresql.toString());
	}
}